package com.ios.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ios.dto.DisablerExecution;
import com.ios.dto.RelativeExecution;
import com.ios.entity.DisablerUser;
import com.ios.entity.RelativeUser;
import com.ios.enums.DisablerStateEnum;
import com.ios.enums.RelativeStateEnum;
import com.ios.service.IDisablerService;
import com.ios.service.IRelativeService;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/login",method = {RequestMethod.POST})
public class LoginController {

    @Autowired
    private IDisablerService disablerService;
    @Autowired
    private IRelativeService relativeService;

    @RequestMapping(value = "/disabler")
    @ResponseBody
    public Map<String,Object> loginDisabler(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String userName = HttpServletRequestUtil.getString(request, "username");
        String password = HttpServletRequestUtil.getString(request, "password");
        if (userName !=null && password !=null){
            DisablerExecution de = disablerService.LoginDisablerUser(userName,password);
            if (de.getState()==DisablerStateEnum.SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("LoginRecord",de.getLoginRecord());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",de.getStateInfo());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/checkdisabler")
    @ResponseBody
    public Map<String,Object> CheckLoginDisabler(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int id = HttpServletRequestUtil.getInt(request,"id");
        if (id != -1){
            DisablerExecution de = disablerService.CheckLoginDisablerUser(id);
            if (de.getState()==DisablerStateEnum.SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("DisablerInfo",de.getDisablerUser());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",de.getStateInfo());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/relative")
    @ResponseBody
    public Map<String,Object> loginRelative(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String userName = HttpServletRequestUtil.getString(request, "username");
        String password = HttpServletRequestUtil.getString(request, "password");
        if (userName !=null && password !=null) {
            RelativeExecution re = relativeService.LoginRelativeUser(userName, password);
            if (re.getState() == RelativeStateEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
                modelMap.put("LoginRecord", re.getLoginRecord());
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", re.getStateInfo());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/checkrelative")
    @ResponseBody
    public Map<String,Object> CheckLoginRelative(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int id = HttpServletRequestUtil.getInt(request,"id");
        if (id != -1){
            RelativeExecution re = relativeService.CheckLoginRelativeUser(id);
            if (re.getState()==RelativeStateEnum.SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("RelativeInfo",re.getRelativeUser());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",re.getStateInfo());
            }
        }
        return modelMap;
    }
}

